﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Blog.ViewModel.SysModule;
using Leo.Blog.DAL.SysModule;
using Leo.Blog.Model;

namespace Leo.Blog.BLL.SysModule
{
    public class Sys_Roles_B
    {
        public static List<SysRoleVM> GetRoles(int pageIndex, int pageSize, out int totalCount)
        {

            Sys_Roles_D _sysRolesD = new Sys_Roles_D();
            List<Sys_Roles> listSysRoles = _sysRolesD.LoadPageEntities(pageIndex, pageSize, out totalCount,
                    null, true, m => m.RoleId).ToList();

            var listSysRoleVM = (from lsm in listSysRoles
                                   select new SysRoleVM
                                   {
                                       RoleId=lsm.RoleId,
                                       RoleName=lsm.RoleName,
                                       RoleDesc=lsm.RoleDesc,
                                       IsEnable = lsm.IsEnable,
                                       CreateBy = lsm.CreateBy,
                                       CreateDT = lsm.CreateDT,
                                       LastEditBy=lsm.LastEditBy,
                                       LastEditDT=lsm.LastEditDT
                                   }).ToList();
            return listSysRoleVM;
        }

        public static SysRoleVM AddorUpdate(SysRoleVM model)
        {
            Sys_Roles_D _sysRolesD = new Sys_Roles_D();
            Sys_Roles role = _sysRolesD.FindSingle(m => m.RoleId == model.RoleId);
            if (role == null)
            {
                #region 增加
                Sys_Roles sysRole = new Sys_Roles
                {RoleName=model.RoleName,
                    RoleDesc=model.RoleDesc,
                    IsEnable = model.IsEnable,
                    CreateBy = model.CreateBy,
                    CreateDT = DateTime.Now,
                    LastEditBy = model.CreateBy,
                    LastEditDT = DateTime.Now
                };
                Sys_Roles obj = _sysRolesD.AddEntity(sysRole);
                if (obj != null)
                {
                    return model;
                }
                return null;
                #endregion
            }
            else
            {
                #region 修改
                role.RoleName = model.RoleName;
                role.RoleDesc = model.RoleDesc;
                role.IsEnable = model.IsEnable;
                role.LastEditBy = model.CreateBy;
                role.LastEditDT = DateTime.Now;

                if (_sysRolesD.UpdateEntity(role))
                {
                    return model;
                }
                return null;
                #endregion
            }
        }

        public static bool UpdateIsEnable(int roleId, int userId)
        {
            Sys_Roles_D _sysRolesD = new Sys_Roles_D();
            Sys_Roles role = _sysRolesD.FindSingle(m => m.RoleId == roleId);
            if (role != null)
            {
                role.IsEnable = role.IsEnable == 1 ? 0 : 1;
                role.LastEditBy = userId;
                role.LastEditDT = DateTime.Now;
                return _sysRolesD.UpdateEntity(role);
            }
            return false;
        }

        public static bool Delete(int roleId)
        {
            Sys_Roles_D _sysRolesD = new Sys_Roles_D();
            Sys_Roles role = _sysRolesD.FindSingle(m => m.RoleId == roleId);
            if (role != null)
            {
                return _sysRolesD.DeleteEntity(role);
            }
            return false;
        }

        public static bool DeleteList(string ids)
        {
            List<Sys_Roles> roleList = new List<Sys_Roles>();
            foreach (string id in ids.Split(','))
            {
                if (string.IsNullOrEmpty(id)) { continue; }
                roleList.Add(new Sys_Roles { RoleId = int.Parse(id) });
            }
            Sys_Roles_D _sysRolesD = new Sys_Roles_D();
            return _sysRolesD.DeleteList(roleList);
        }

    }
}
